#include "Log.h"
#include "../Error.h"

Logger& lout = Logger::getInstance();

Logger& Logger::getInstance()
{
	static Logger instance;
	return instance;
}

Logger::Logger()
: lock()
#if WRITE_LOG_FILE
, fout("log.txt")
#endif
{
	CHECK(pthread_mutex_init(&lock, NULL));
}

Logger::~Logger()
{
	//Make sure that anyone trying to use the logger is done
	pthread_mutex_lock(&lock);
	pthread_mutex_unlock(&lock);
#if WRITE_TO_COUT
	std::cout.flush();
#endif
#if WRITE_LOG_FILE
	fout.flush();
	fout.close();
#endif
	CHECK(pthread_mutex_destroy(&lock));
}

